package com.nulldev.util.graphics.renderIt.util;

import java.util.concurrent.atomic.AtomicInteger;

import com.nulldev.util.VariableAPI.MathUtil;

public class renderPosition {
	private final AtomicInteger x = new AtomicInteger(5);
	private final AtomicInteger y = new AtomicInteger(0);
	private final int initial_x, initial_y;

	public renderPosition() {
		this.initial_x = 5;
		this.initial_y = 0;
	}

	public renderPosition(final int x, final int y) {
		this.initial_x = x;
		this.initial_y = y;
		if (x >= 0)
			this.x.set(x);
		if (y >= 0)
			this.y.set(y);
	}

	public int appendGetY(final int y) {
		return this.y.getAndAdd(y);
	}

	public int appendGetX(final int x) {
		return this.x.getAndAdd(x);
	}

	public renderPosition addX(final int x) {
		this.x.addAndGet(x);
		return this;
	}

	public renderPosition addY(final int y) {
		this.y.addAndGet(y);
		return this;
	}

	public int getY() {
		return this.y.get();
	}

	public int getX() {
		return this.x.get();
	}

	public void setX(final int x) {
		if (x > -1) {
			this.x.set(x);
		}
	}

	public void setY(final int y) {
		if (y > -1) {
			this.y.set(y);
		}
	}

	public void resetX() {
		this.x.set(this.initial_x);
	}

	public void resetY() {
		this.y.set(this.initial_y);
	}

	public void set(final renderPosition rp) {
		if (rp == null)
			return;
		this.x.set(rp.x.get());
		this.y.set(rp.y.get());
	}

	@Override
	public String toString() {
		return "renderPosition[hash=" + MathUtil.toHex(super.hashCode()) + ",x=" + this.x + ",y=" + this.y + "]";
	}

	public renderPosition clone() {
		return new renderPosition(this.x.get(), this.y.get());
	}

	public renderPosition readOnly() {
		return new renderPosition() {
			@Override
			public int appendGetX(int x) {
				return super.x.get();
			}

			@Override
			public int appendGetY(int y) {
				return super.y.get();
			}

			@Override
			public void setX(int x) {
			}

			@Override
			public void setY(int y) {
			}

			@Override
			public void set(renderPosition rp) {
			}
		};
	}

	public renderPosition subtractY(final int y) {
		this.y.getAndAdd(-y);
		return this;
	}

	public renderPosition subtractX(final int x) {
		this.x.getAndAdd(-x);
		return this;
	}

	public renderPosition applyX(final int x) {
		this.x.set(x);
		return this;
	}

	public renderPosition applyY(final int y) {
		this.y.set(y);
		return this;
	}

	public renderPosition setAndResetX() {
		this.x.set(this.initial_x);
		return this;
	}

	public renderPosition setAndResetY() {
		this.y.set(this.initial_y);
		return this;
	}
}
